Skip to content

feat(bus,cli,config): keep business events on disk so the history survives a restart - #184

Merged
V3RON merged 3 commits into
mainfrom
feature/168
Oct 1, 2026
Merged

V3RON merged 3 commits into
mainfrom
feature/168

Conversation

@V3RON

@V3RON V3RON commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Closes #168

Every business event is now also written to events.jsonl in the data directory, so the history survives a restart or crash. events.replay with sinceTs reads the file in both dispatchers through one EventHistory. simlock events --since reads the file directly when no daemon answers. --follow subscribes before it replays, so where history meets the live stream nothing is lost or printed twice. The file rotates on the new eventLog.rotateBytes (5 MiB, one generation kept) and is separate from daemon.log, which no longer copies component.installed or device.slimmed. If the file can't be opened or written, the daemon logs one line and replays from memory.

ADR 0006 stays Accepted — not yet implemented until #169 lands.

Completion conditions

  • Lease, restart: lease.granted with its timestamp is still in the file — e2e event-history, "prints a lease granted before a daemon restart…"
  • Every line is JSON with seq, timestamp, event, payload, module — e2e "writes every event file line as JSON…"
  • Small limit: file + .1 ≤ 2× limit, newest present — e2e "keeps events.jsonl plus events.jsonl.1 under twice…"
  • daemon.log rotation removes nothing from the event file — e2e "keeps every emitted event … after daemon.log rotates"
  • Unwritable event file: lease granted and released — e2e "grants and releases a lease with a directory at the event file's path"
  • Restart, events --since 1h prints the earlier grant — same first e2e; HTTP: e2e http-api, "GET /v1/events?since=1h returns an event from before a daemon restart"
  • Daemon stopped: --since 1h prints history, no daemon started — e2e "prints the history with the daemon stopped…"
  • --since 1h --follow: history once, then live — unit cli "events --since --follow prints the history once and then the live events"
  • CONFIGURATION.md, CLI.md, EVENTS.md updated — in the diff

pnpm check green. Every new test was checked to fail when the code it covers is broken.

Review

Spec review: 9 findings, 2 fixed. Code review: 10 findings, 5 fixed.

Rejected:

  • readEventFile skips JSON lines that are not envelopes, beyond "not JSON" — a line with no numeric seq/timestamp cannot be filtered or deduplicated.
  • runDaemon moved onto daemonRefusal, beyond the module list — the spec says --since uses the same test daemon status uses; one helper is how (architecture rule 10).
  • writeModule hoisted to file scope in main.test.ts — test plumbing, so the new event-file tests can reuse it.
  • NodeFileLogSink doc comment changed, though the spec says the sink is unchanged — its claims about volume and "built once" became false (architecture rule 13). No code changed.
  • eventKey exported beyond the three names the spec lists — the code review asked for one definition of event identity shared by the bus and the CLI (architecture rule 10).
  • bus/index.ts has a memory-only comment the diff should fix — none exists; the only ring comment is about the ring's count bound, which still holds.
  • Refusal test and runDaemon refactor are outside the spec's lists — without that test the rethrow can be deleted with a green suite (testing rule 3).
  • A crash mid-write merges the next start's first event into the torn line — needs the process to die inside one writeSync; the only cheap fix is a separator line, which breaks "every line parses as JSON".
  • --since prints the file on a transport-kind failure — that is the spec's chosen test, the one daemon status uses for "stopped".
  • The sinceTs predicate exists for both the ring and the file — two stores each filter their own entries; the choice of store is made once, in EventHistory.replay.
  • The --follow join lives in the CLI (architecture rule 8) — the spec puts the join in runEvents; the daemon contract is unchanged.
  • An unrelated e2e timeout (capacity-cleanup-nuke) under load — passes alone on this commit and on main.

Written by an agent.

V3RON added 3 commits October 1, 2026 13:44
…vives a restart (#168)

Every event the daemon emits is also written to events.jsonl in the data
directory, capped by eventLog.rotateBytes with one rotated generation.
events.replay with sinceTs reads the file in both dispatchers; the CLI
reads it directly for events --since when no daemon answers, and
events --follow subscribes before replaying so the join loses and repeats
nothing. daemon.log no longer copies component.installed and
device.slimmed (ADR 0006).
@V3RON
V3RON merged commit f607c67 into main Oct 1, 2026
14 checks passed
@V3RON
V3RON deleted the feature/168 branch October 1, 2026 12:27
V3RON added a commit that referenced this pull request Oct 1, 2026
…lure; daemon logs --follow (#187)

Closes #169

`daemon.log` now records what the daemon was asked to do and what went
wrong (ADR 0006).

- Every dispatched call leaves one `operation` line, built only in
`runDispatch`, for socket, MCP, HTTP and gateway dispatch. Its level
comes from the new contract `effect`: write at info, read at debug,
failure at info, `INTERNAL` at error.
- Core modules log the background failures they handle, with device,
step, error, and lease or requester where known.
- At `debug`, `LoggingProcessRunner` logs each device command with its
exit code. It never logs env, input or output.
- A failing event subscriber is one JSON line through the logger.
- `simlock daemon logs --follow` polls through `Filesystem`/`Clock`,
detects rotation from `daemon.log.1` changing identity, and waits for a
missing file.

Deviations from the spec:
- `doctor --purge-orphans` counts as a write, because it destroys
devices.
- A call refused before input validation logs its code but no message,
because that message quotes raw wire input.

## Completion conditions

- [x] Lease and release over the socket each leave one line with
requester and duration. `e2e/daemon-log.test.ts`
- [x] Unknown model: the line carries `UNKNOWN_MODEL`. e2e
- [x] `status` adds no line at the default level and one at `debug`. e2e
- [x] Boot failure: a line with the device, `step: boot` and the
driver's error text. e2e and unit
- [x] At `debug`, each device command leaves a line with its exit code.
e2e (`device.exec`) and unit
- [x] A throwing subscriber gives one JSON line and nothing on
`console.error`. `main.test.ts` through `startDaemon`
- [x] `--follow` prints a later lease's line, and keeps printing after
rotation with a small `log.rotateBytes`. e2e and unit
- [x] CLI.md describes the log and `--follow`.
- [x] ADR 0006 and the ADR index say _Accepted_: #168 landed first
(#184), so this PR closes the second.
- [x] `pnpm check` green. Each new test was seen failing with its code
removed.

## Review

Spec review: 16 findings over two rounds, 6 fixed. Code review: 10
findings over two rounds, 9 fixed.

Rejected:

- `doctor.run` with `purgeOrphans` is a write — it destroys devices, and
the Outcome says every state change leaves a line. Spec needs:
`purgeOrphans` in the effect list.
- Failure message missing before input validation — safety rule 10: the
message quotes raw wire input. Spec needs: `message` only after
validation.
- `log.rotateBytes` row added to CONFIGURATION.md — neither `log.*` key
was documented, and the log paragraph names this one.
- `checkAccess`/`authorizeContext` split out of `runDispatch` — required
by the pre-commit complexity gate. Behaviour unchanged.
- Two `fallow-ignore` comment edits — the pre-commit audit flags them in
files this diff touches.
- Tests beyond the spec's list — they cover paths the code review found
untested (testing rule 3).
- Worker dispatcher gets `classifyError` in `server.ts`, not `main.ts` —
`server.ts` builds that dispatcher and already imports it. Same effect.
- A `cleanup.run` that joins a failing scheduled run gets both lines —
the background run failed and the request is a separate record.

*Written by an agent.*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep business events on disk so the history survives a restart

1 participant